Repository navigation
Keep the union order in the union cache key - #818
Merged
Merged
Conversation
typing.Union compares and hashes equal regardless of argument order, so after yukinarit#665 UnionCacheKey returned the wrapper generated for the first ordering seen, while the function name is built from the actual order. Serializing or deserializing the other ordering then failed with a KeyError/SerdeError such as 'union_se_str_int'. Add repr(union_cls) to the key; keeping the type itself still separates same-named classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Union[int, str] == Union[str, int](and the hashes match), so since #665 the top-level union cache returns the wrapper generated for whichever ordering was used first. The function name is still built from the actual argument order, so the other ordering misses it for the rest of the process:The same happens through
from_json/to_jsonand forlist[Union[...]]/dict[str, Union[...]]elements. This is the case #448 fixed with the old string key; #764 handled the same order-blind equality foris_opt.This adds
repr(union_cls)toUnionCacheKey. The type stays in the key, so classes that share a name are still kept apart as #665 intended. Two tests are added next totest_union_with_different_ordering: both orderings throughto_json/from_json(fails on main), and same-named classes in a union (fails if the key keeps only the repr). The full test suite, black and ruff pass.Written with AI assistance (Claude); I have reviewed the change.